package com.chb.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.chb.dao.DepartmentDao;
import com.chb.dao.StaffDao;
import com.chb.dao.StaffTransferDao;
import com.chb.model.entity.*;
import com.chb.model.vo.PageBean;
import com.chb.model.vo.Result;
import com.chb.model.vo.StaffVo;
import com.chb.service.StaffService;
import com.chb.utils.MathUtils;
import com.chb.utils.SMSUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author: 陈海彬
 * @date: Created in 2023/9/25 15:26
 * @description:
 * @modified By:
 * @version:
 */
@Service
public
class StaffServiceImpl implements StaffService {
    @Autowired
    private StaffDao staffDao;
    @Autowired
    private DepartmentDao departmentDao;
    @Autowired
    private HttpSession session;
    @Autowired
    private StaffTransferDao staffTransferDao;

    /**
     * @param name 账号
     * @param pwd  密码
     * @return 用户信息
     * @create by: 陈海彬
     * @description: 根据账号密码获取用户信息
     * @create time: 2023/9/25 15:27
     */
    @Override
    public Result selectByNameAndPwd(String name, String pwd) {
        //用户信息
        Staff staff = staffDao.selectByNameAndPwd(name, pwd);

        //账号或密码错误
        if (staff == null) {
            return new Result(500, "账号或密码错误", null);
        }
        //表示冻结
        if (staff.getState() == 2) {
            return new Result(500, "账号被冻结，请联系管理员解冻", null);
        }
        //表示离职
        if (staff.getState() == 0) {
            return new Result(500, "账号已离职，请联系管理员解决", null);
        }
        //把当前用户放入session中
        session.setAttribute("staff", staff);
        return Result.success();
    }

    /**
     * @param
     * @return
     * @create by: 张77
     * @description: 查询所有员工信息+分页
     * @create time: 2023/9/26 16:20
     */
    @Override
    public Result findAllStaffInfo(Integer current, Integer pageSize, StaffVo staffVo) {
        Staff staff = (Staff) session.getAttribute("staff");
        PageHelper.startPage(current, pageSize);
        if (!staff.getStaffid().equals(111)) {
            staffVo.setBranchid(staff.getBranchid());
        }
        List<Staff> list = staffDao.findAllStaffInfo(staffVo);
        PageInfo<Staff> pageInfo = new PageInfo<Staff>(list);
        PageBean pageBean = new PageBean<>(pageInfo.getTotal(), pageInfo.getList());
        return new Result(200, "查询成功", pageBean);
    }

    /**
     * @param
     * @return
     * @create by: 张77
     * @description: 查询所有员工信息+分页
     * @create time: 2023/9/26 16:20
     */
    @Override
    public Result findAllStaffInfo(Integer current, Integer pageSize) {
        PageHelper.startPage(current, pageSize);
//        List<Staff> list = staffDao.findAllStaffInfo();
//        PageInfo<Staff> pageInfo = new PageInfo<Staff>(list);
//        PageBean pageBean = new PageBean<>(pageInfo.getTotal(), pageInfo.getList());
//        return new Result(200, "查询成功", pageBean);
        return Result.success();
    }


    /**
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 获取用户基本信息
     * @create time: 2023/9/27 16:08
     */
    @Override
    public Result getUserInfo() {
        Staff staff = (Staff) session.getAttribute("staff");
        return Result.success(staff);
    }


    /**
     * @return com.chb.model.vo.Result
     * @create by: 张77
     * @description: 获取所有部门名称信息
     * @create time: 2023/10/7 11:04
     */
    @Override
    public Result getDeptment() {
        List<Department> departments = departmentDao.selectAllDeptName();
        return Result.success(departments);
    }

    /**
     * @param
     * @return
     * @create by: 张77
     * @description: 查询所有状态信息
     * @create time: 2023/10/8 15:50
     */
    @Override
    public Result getState() {
        return null;
    }


    /**
     * @param staff 员工信息
     * @return com.chb.model.vo.Result
     * @create by: 张77
     * @description: 修改员工信息
     * @create time: 2023/10/7 17:11
     */
    @Override
    @Transactional
    public Result updateStaff(Staff staff) {
        Integer state = staff.getState();
        if (state != null) {
            if (state.equals(0)) {
                //设置离职时间
                staff.setLeavetime(new Date(System.currentTimeMillis()));
            }
        }
        Integer integer = staffDao.updateStaff(staff);
        if (integer > 0) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * @param oldPassWord 用户输入的旧密码
     * @param newPassWord 新密码
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 修改用户密码
     * @create time: 2023/10/10 9:49
     */
    @Override
    @Transactional
    public Result updatePassWord(String oldPassWord, String newPassWord) {
        Staff staff = (Staff) session.getAttribute("staff");
        //判断旧密码是否一致
        Staff newStaff = staffDao.selectByNameAndPwd(staff.getStaffname(), oldPassWord);
        if (newStaff == null) {
            return new Result(202, "密码格式错误或者密码与原密码不符！", null);
        }

        // 进行修改密码
        staff.setPassword(newPassWord);
        staffDao.updatePassWord(staff);
        // 清除session
        session.removeAttribute("staff");
        return new Result(200, "密码修改成功", null);
    }

    /**
     * @create by: 陈海彬
     * @description: 退出登录
     * @create time: 2023/10/10 10:19
     */
    @Override
    public Result loginOut() {
        session.removeAttribute("staff");
        return Result.success();
    }

    /**
     * @param phone 要验证的手机号
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 发送验证码
     * @create time: 2023/10/11 12:19
     */
    @Override
    public Result sendValidateCode(String phone) {
        Staff staff = staffDao.getStaffByPhone(phone);
        if (staff != null) {
            String randomCode = MathUtils.randomCode();  // 生成随机数
            session.setAttribute("staff" + phone, randomCode);
            try {
                SMSUtils.sendShortMessage("SMS_463632296", phone, randomCode);
            } catch (ClientException e) {
                e.printStackTrace();
            }
            return new Result(200, "发送验证码成功！", null);
        }
        return new Result(202, "验证码发送失败，请检查手机号输入是否正确", null);
    }


    /**
     * @param phone 手机号
     * @param code  验证码
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 校验登录
     * @create time: 2023/10/11 15:34
     */
    @Override
    public Result identityVerification(String phone, String code) {
        //获取session
        Object staff = session.getAttribute("staff" + phone);
        if (staff != null) {
            if (code.equals(staff.toString())) {
                Staff staff1 = staffDao.getStaffByPhone(phone);
                staff1.setPassword("111");
                staffDao.updatePassWord(staff1);
                session.removeAttribute("staff" + phone);
                return new Result(200, "校验成功！密码已经修改为111", null);
            } else {
                return new Result(203, "验证码错误，请重新输入！", null);
            }
        } else {
            return new Result(202, "请检查手机号输入是否正确！", null);
        }
    }


    /**
     * @param staffid 用户id
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 判断该用户是否可以被平调
     * @create time: 2023/10/12 10:34
     */
    @Override
    public Result transferJudgment(Integer staffid) {
        StaffTransfer staffTransfer = staffTransferDao.selectStaffTransferByInNow(staffid);
        if (staffTransfer != null) {
            return new Result(202, "该员工正在调动！", null);
        }
        Staff staffByBranchId = staffDao.getStaffByBranchId(staffid);
        if (staffByBranchId != null) {
            return new Result(203, "该员工为店长，无法调动！", null);
        }
        return Result.success();
    }


    /**
     * @param staff
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 确认调离
     * @create time: 2023/10/12 10:54
     */
    @Override
    public Result transferConfirmed(Staff staff) {
        Staff session1 = (Staff) session.getAttribute("staff");
        StaffTransfer staffTransfer = new StaffTransfer();
        staffTransfer.setStaffid(staff.getStaffid());                     //调离id
        staffTransfer.setBranchid(staff.getBranchid());                   //原始分店信息
        staffTransfer.setDeptid(staff.getDeptid());                       //原始部门
        staffTransfer.setNewbranchid(staff.getNewbranchid());             //新分店信息
        staffTransfer.setNewdeptid(staff.getNewdeptid());                 //新部门信息
        staffTransfer.setApplytime(new Date(System.currentTimeMillis())); //平调时间
        staffTransfer.setApplyid(session1.getStaffid());                  //操作者
        staffTransfer.setState(1);//平调状态
        Integer integer = staffTransferDao.addStaffTransfer(staffTransfer);
        if (integer > 0) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * @param phone   手机号
     * @param staffid
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 校验手机号是否重复
     * @create time: 2023/10/12 12:26
     */
    @Override
    public Result phoneIsReplace(String phone, Integer staffid) {
        if (staffid != null) {
            Staff andStaffid = staffDao.getStaffByPhoneAndStaffid(phone, staffid);
            if (andStaffid != null) {
                return Result.success();
            }
        }
        Staff staff = staffDao.getStaffByPhone(phone);
        if (staff != null) {
            return new Result(202, "手机号已存在，请重新输入", null);
        }
        return Result.success();
    }

    /**
     * @param staffid
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 判断用户名是否重复
     * @create time: 2023/10/12 14:49
     */
    @Override
    public Result staffidIsReplace(Integer staffid) {
        Staff staff = staffDao.findStaffByStaffId(staffid);
        if (staff != null) {
            return new Result(202, "工号重复，请重新输入", null);
        }
        return Result.success();
    }

    /**
     * @param idCar
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 判断身份证是否重复
     * @create time: 2023/10/12 14:55
     */
    @Override
    public Result checkIdCar(String idCar) {
        Staff staff = staffDao.findStaffByIdCar(idCar);
        if (staff != null) {
            return new Result(202, "身份证号码，请重新输入", null);
        }
        return Result.success();
    }

    /**
     * @param staff
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 添加员工
     * @create time: 2023/10/12 15:00
     */
    @Override
    public Result addStaff(Staff staff) {
        Branch branch = staff.getBranch();
        Integer branchid = branch.getBranchid();
        if (branchid != null) {
            staff.setBranchid(branchid);
        }
        /*Department department = staff.getDepartment();
        if (department != null) {
            staff.setDeptid(department.getDeptid());
//            staff.getRoleid(department.getRoleid());

        }*/
        Integer index = staffDao.addStaff(staff);
        if (index > 0) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * @param
     * @return
     * @create by: 张77
     * @description:新增下拉框查的部门
     * @create time: 2023/10/12 19:38
     */
    @Override
    public Result addfendAllDept() {
        List<Dept> addDept = staffDao.addfendAllDept();
        return new Result(200, "查询成功", addDept);
    }

    /**
     * @param
     * @return
     * @create by: 张77
     * @description: 新增下拉框职位
     * @create time: 2023/10/12 20:00
     */
    @Override
    public Result addfendAllRole() {
        List<Role> addDept = staffDao.getBranchByStatus();
        return new Result(200, "查询成功", addDept);
    }

//    /**
//     * @create by: xIAO_ZHOU
//     * @description: 调离后将原来店铺的店长branchid改为 新店的branchid
//     * @create time: 2023/10/10 11:46
//     * @param
//     * @return
//     */
//    @Override
//    public Integer changeManagerBranchid(Staff staff) {
//        Integer integer = staffDao.changeManagerBranchid(staff);
//        return integer;
//    }
//
//    /**
//     * @create by: xIAO_ZHOU
//     * @description: 调离后将原来店铺的店长deptid改为 新店的deptid
//     * @create time: 2023/10/10 11:46
//     * @param
//     * @return
//     */
//    @Override
//    public Integer changeManagerDeptid(Staff staff) {
//        Integer integer = staffDao.changeManagerDeptid(staff);
//        return integer;
//    }

}


